package org.tobuy.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.tobuy.dao.IAuctionDAO;
import org.tobuy.vo.Auction;
import org.tobuy.vo.User;


public class AuctionDAOImpl implements IAuctionDAO{
	
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	ResultSet rs = null;
	public AuctionDAOImpl(Connection conn){
		this.conn = conn;
	}

	
	
	private Auction getAuctionFromRs(ResultSet rs){
		Auction auction = new Auction();
		
		try{
			auction.setItem_id(rs.getInt("itemid"));
			auction.setBuyer_id(rs.getString("buyerid"));
			auction.setSeller_id(rs.getString("sellerid"));
			auction.setBuyer_offer(rs.getDouble("buyeroffer"));
			auction.setOffer_time(rs.getTimestamp("offertime"));
			auction.setExp_time(rs.getTimestamp("exptime"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return auction;
	}



	public List<Auction> findAuctions() throws Exception {
		List<Auction> auctions = new ArrayList<Auction>();
		String sql = "Select * from auction";
		this.pstmt = this.conn.prepareStatement(sql);
		rs = this.pstmt.executeQuery();
		while(rs.next()){
			Auction auction = getAuctionFromRs(rs);
			auctions.add(auction);
		}
		this.pstmt.close();
		return auctions;
	}



	public List<Auction> findBuyerAuctions(User user) throws Exception {
		List<Auction> auctions = new ArrayList<Auction>();
		String sql = "Select * from auction where buyerid=?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setString(1, user.getUser_id());
		rs = this.pstmt.executeQuery();
		while(rs.next()){
			Auction auction = getAuctionFromRs(rs);
			auctions.add(auction);
		}
		this.pstmt.close();
		return auctions;
	}
	
	
	public List<Auction> findSellerAuctions(User user) throws Exception {
		List<Auction> auctions = new ArrayList<Auction>();
		String sql = "Select * from auction where sellerid=?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setString(1, user.getUser_id());
		rs = this.pstmt.executeQuery();
		while(rs.next()){
			Auction auction = getAuctionFromRs(rs);
			auctions.add(auction);
		}
		this.pstmt.close();
		return auctions;
	}

	
	public boolean addAuction(Auction auction) throws Exception {
		boolean flag = false;
		String sql = "INSERT INTO auction(itemid,buyerid,sellerid,buyeroffer,offertime,exptime) VALUES(?,?,?,?,?,?)";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, auction.getItem_id());
		this.pstmt.setString(2, auction.getBuyer_id());
		this.pstmt.setString(3, auction.getSeller_id());
		this.pstmt.setDouble(4, auction.getBuyer_offer());
		this.pstmt.setTimestamp(5, auction.getOffer_time());
		this.pstmt.setTimestamp(6, auction.getExp_time());
		if(pstmt.executeUpdate()!=0){
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}


	public boolean updateAuction(String item_id, double price, String buyer_id)
			throws Exception {
		boolean flag = false;
		String sql = "UPDATE auction SET buyeroffer=?, buyerid=?, offertime=? where itemid=?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setDouble(1, price);
		this.pstmt.setString(2, buyer_id);
		this.pstmt.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
		this.pstmt.setInt(4, Integer.parseInt(item_id));
		if(pstmt.executeUpdate()!=0){
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}



	public boolean deleteAuction(String item_id) throws Exception {
		boolean flag = false;
		String sql = "DELETE FROM auction where itemid=?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, Integer.parseInt(item_id));
		if(pstmt.executeUpdate()!=0){
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}



	public Auction findAuction(String item_id) throws Exception {
		Auction auction = new Auction();
		String sql = "Select * from auction where itemid=?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, Integer.parseInt(item_id));
		rs = this.pstmt.executeQuery();
		rs.next();
		auction = getAuctionFromRs(rs);
		this.pstmt.close();
		return auction;
	}



	
}
